/*
 * Copyright (c) Jiandong.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import hilog from '@ohos.hilog';

import { ReactStylesDiffMap } from './ReactStylesDiffMap';
import { ThemedReactContext } from './ThemedReactContext';

import { DVModel, DVModelContainer, DVModelParameters, DVModelEvents, DVModelChildren } from "./DynamicView"

export interface NativeUIManagerMessage {
  message: number
  tag: number
  type: string,
  style?: Object,
  Event?: Object
}

export class NativeViewRenderManager {
  private static readonly TAG = "react NativeViewRenderManager";
  private static readonly CREATEVIEW = 0;
  private static readonly UPDATEPROPERTIES = 1;
  private static readonly DELETEVIEW = 2
  private static readonly BATCHRENDER = 3
  private mContainer: DVModelContainer = null;

  public attachReactRoot(moduleContainer: DVModelContainer) {
    this.mContainer = moduleContainer;
  }

  public processMessage(uiMessage: NativeUIManagerMessage): void {
    hilog.info(0x0000, NativeViewRenderManager.TAG, "processMessage  111");
    if (!this.mContainer) return;
    hilog.info(0x0000, NativeViewRenderManager.TAG, "processMessage 222 %{public}d", uiMessage.message as number);

    switch (uiMessage.message as number) {
      case NativeViewRenderManager.CREATEVIEW:
        this.createView(null, uiMessage.tag, uiMessage.type)
        break;
      case NativeViewRenderManager.UPDATEPROPERTIES:
        this.updateProperties(uiMessage.tag, uiMessage.style as ReactStylesDiffMap)
        break;
      case NativeViewRenderManager.DELETEVIEW:
        this.deleteView(uiMessage.tag, null)
        break;
      case NativeViewRenderManager.BATCHRENDER:
        this.batchRender();
        break;
      default:
        break;
    }
  }

  public createView(themedContext: ThemedReactContext, tag: number, type: string, initialProps?: ReactStylesDiffMap): void {
    hilog.info(0x0000, NativeViewRenderManager.TAG, "createView, tag %{public}s %{public}s: ", tag, type);
    this.mContainer?.append(new DVModel(tag, type, null, null, null));
  }

  public updateProperties(tag: number, props: ReactStylesDiffMap) {
    hilog.info(0x0000, NativeViewRenderManager.TAG, `updateProperties`)
    this.mContainer?.updateProperties(tag, null);
  }

  public deleteView(tag: number, nodes: Array<{pid: number, id: number }>) {
    hilog.info(0x0000, NativeViewRenderManager.TAG, `deleteView`)
    for (const node of nodes) {
      this.mContainer?.delete(tag);
    }
  }

  public batchRender() {
    hilog.info(0x0000, NativeViewRenderManager.TAG, `batchRender`)
  }

  public dispatchViewManagerCommand() {
    hilog.info(0x0000, NativeViewRenderManager.TAG, `dispatchViewManagerCommand`)
  }

  public measure() {
    hilog.info(0x0000, NativeViewRenderManager.TAG, `measure`)
  }

  public measureInWindow() {
    hilog.info(0x0000, NativeViewRenderManager.TAG, `measureInWindow`)
  }

  public measureLayout() {
    hilog.info(0x0000, NativeViewRenderManager.TAG, `measureLayout`)
  }
}





